home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / c / make-3.75 / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  16.2 KB  |  663 lines

  1. /* Directory hashing for GNU Make.
  2. Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20.  
  21. #if    defined (POSIX) || defined (HAVE_DIRENT_H) || defined (__GNU_LIBRARY__)
  22. #include <dirent.h>
  23. #ifndef __GNU_LIBRARY__
  24. #define D_NAMLEN(d) strlen((d)->d_name)
  25. #else    /* GNU C library.  */
  26. #define D_NAMLEN(d) ((d)->d_namlen)
  27. #endif    /* Not GNU C library.  */
  28. #else    /* Not POSIX or HAVE_DIRENT_H.    */
  29. #define direct dirent
  30. #define D_NAMLEN(d) ((d)->d_namlen)
  31. #ifdef    HAVE_SYS_NDIR_H
  32. #include <sys/ndir.h>
  33. #endif    /* HAVE_SYS_NDIR_H */
  34. #ifdef    HAVE_SYS_DIR_H
  35. #include <sys/dir.h>
  36. #endif    /* HAVE_SYS_DIR_H */
  37. #ifdef HAVE_NDIR_H
  38. #include <ndir.h>
  39. #endif    /* HAVE_NDIR_H */
  40. #endif    /* POSIX or HAVE_DIRENT_H or __GNU_LIBRARY__.  */
  41.  
  42. #if defined (POSIX) && !defined (__GNU_LIBRARY__)
  43. /* Posix does not require that the d_ino field be present, and some
  44.    systems do not provide it. */
  45. #define REAL_DIR_ENTRY(dp) 1
  46. #else
  47. #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
  48. #endif /* POSIX */
  49.  
  50. #ifdef __MSDOS__
  51. #include <ctype.h>
  52.  
  53. static char *
  54. dosify (filename)
  55.      char *filename;
  56. {
  57.   static char dos_filename[14];
  58.   char *df;
  59.   int i;
  60.  
  61.   if (filename == 0)
  62.     return 0;
  63.  
  64.   if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
  65.     return filename;
  66.  
  67.   df = dos_filename;
  68.  
  69.   /* First, transform the name part.  */
  70.   for (i = 0; *filename != '\0' && i < 8 && *filename != '.'; ++i)
  71.     *df++ = tolower (*filename++);
  72.  
  73.   /* Now skip to the next dot.    */
  74.   while (*filename != '\0' && *filename != '.')
  75.     ++filename;
  76.   if (*filename != '\0')
  77.     {
  78.       *df++ = *filename++;
  79.       for (i = 0; *filename != '\0' && i < 3 && *filename != '.'; ++i)
  80.     *df++ = tolower (*filename++);
  81.     }
  82.  
  83.   /* Look for more dots.  */
  84.   while (*filename != '\0' && *filename != '.')
  85.     ++filename;
  86.   if (*filename == '.')
  87.     return filename;
  88.   *df = 0;
  89.   return dos_filename;
  90. }
  91. #endif
  92. #ifdef _AMIGA
  93. #include <ctype.h>
  94.  
  95. static char *
  96. amigafy (filename)
  97.      char *filename;
  98. {
  99.   static char amiga_filename[136];
  100.   char *df;
  101.   int i;
  102.  
  103.   if (filename == 0)
  104.     return 0;
  105.  
  106.   df = amiga_filename;
  107.  
  108.   /* First, transform the name part.  */
  109.   for (i = 0; *filename != '\0'; ++i)
  110.   {
  111.     *df++ = tolower (*filename);
  112.     ++filename;
  113.   }
  114.  
  115.   *df = 0;
  116.  
  117.   return amiga_filename;
  118. }
  119. #endif
  120.  
  121. /* Hash table of directories.  */
  122.  
  123. #ifndef DIRECTORY_BUCKETS
  124. #define DIRECTORY_BUCKETS 199
  125. #endif
  126.  
  127. struct directory_contents
  128.   {
  129.     struct directory_contents *next;
  130.  
  131.     int dev, ino;        /* Device and inode numbers of this dir.  */
  132.  
  133.     struct dirfile **files;    /* Files in this directory.  */
  134.     DIR *dirstream;        /* Stream reading this directory.  */
  135.   };
  136.  
  137. /* Table of directory contents hashed by device and inode number.  */
  138. static struct directory_contents *directories_contents[DIRECTORY_BUCKETS];
  139.  
  140. struct directory
  141.   {
  142.     struct directory *next;
  143.  
  144.     char *name;         /* Name of the directory.  */
  145.  
  146.     /* The directory's contents.  This data may be shared by several
  147.        entries in the hash table, which refer to the same directory
  148.        (identified uniquely by `dev' and `ino') under different names.  */
  149.     struct directory_contents *contents;
  150.   };
  151.  
  152. /* Table of directories hashed by name.  */
  153. static struct directory *directories[DIRECTORY_BUCKETS];
  154.  
  155.  
  156. /* Never have more than this many directories open at once.  */
  157.  
  158. #define MAX_OPEN_DIRECTORIES 10
  159.  
  160. static unsigned int open_directories = 0;
  161.  
  162.  
  163. /* Hash table of files in each directory.  */
  164.  
  165. struct dirfile
  166.   {
  167.     struct dirfile *next;
  168.     char *name;         /* Name of the file.  */
  169.     char impossible;        /* This file is impossible.  */
  170.   };
  171.  
  172. #ifndef DIRFILE_BUCKETS
  173. #define DIRFILE_BUCKETS 107
  174. #endif
  175.  
  176. static int dir_contents_file_exists_p ();
  177.  
  178. /* Find the directory named NAME and return its `struct directory'.  */
  179.  
  180. static struct directory *
  181. find_directory (name)
  182.      register char *name;
  183. {
  184.   register unsigned int hash = 0;
  185.   register char *p;
  186.   register struct directory *dir;
  187.  
  188.   for (p = name; *p != '\0'; ++p)
  189. #ifdef _AMIGA
  190.     HASH (hash, tolower(*p));
  191. #else
  192.     HASH (hash, *p);
  193. #endif
  194.   hash %= DIRECTORY_BUCKETS;
  195.  
  196.   for (dir = directories[hash]; dir != 0; dir = dir->next)
  197.     if (streq (dir->name, name))
  198.       break;
  199.  
  200.   if (dir == 0)
  201.     {
  202.       struct stat st;
  203.  
  204.       /* The directory was not found.  Create a new entry for it.  */
  205.  
  206.       dir = (struct directory *) xmalloc (sizeof (struct directory));
  207.       dir->next = directories[hash];
  208.       directories[hash] = dir;
  209.       dir->name = savestring (name, p - name);
  210.  
  211.       /* The directory is not in the name hash table.
  212.      Find its device and inode numbers, and look it up by them.  */
  213.  
  214.       if (safe_stat (name, &st) < 0)
  215.     /* Couldn't stat the directory.  Mark this by
  216.        setting the `contents' member to a nil pointer.  */
  217.     dir->contents = 0;
  218.       else
  219.     {
  220.       /* Search the contents hash table; device and inode are the key.  */
  221.  
  222.       struct directory_contents *dc;
  223.  
  224.       hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
  225.       hash %= DIRECTORY_BUCKETS;
  226.  
  227.       for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
  228.         if (dc->dev == st.st_dev && dc->ino == st.st_ino)
  229.           break;
  230.  
  231.       if (dc == 0)
  232.         {
  233.           /* Nope; this really is a directory we haven't seen before.  */
  234.  
  235.           dc = (struct directory_contents *)
  236.         xmalloc (sizeof (struct directory_contents));
  237.  
  238.           /* Enter it in the contents hash table.  */
  239.           dc->dev = st.st_dev;
  240.           dc->ino = st.st_ino;
  241.           dc->next = directories_contents[hash];
  242.           directories_contents[hash] = dc;
  243.  
  244.           dc->dirstream = opendir (name);
  245.           if (dc->dirstream == 0)
  246.         /* Couldn't open the directory.  Mark this by
  247.            setting the `files' member to a nil pointer.  */
  248.         dc->files = 0;
  249.           else
  250.         {
  251.           /* Allocate an array of buckets for files and zero it.  */
  252.           dc->files = (struct dirfile **)
  253.             xmalloc (sizeof (struct dirfile *) * DIRFILE_BUCKETS);
  254.           bzero ((char *) dc->files,
  255.              sizeof (struct dirfile *) * DIRFILE_BUCKETS);
  256.  
  257.           /* Keep track of how many directories are open.  */
  258.           ++open_directories;
  259.           if (open_directories == MAX_OPEN_DIRECTORIES)
  260.             /* We have too many directories open already.
  261.                Read the entire directory and then close it.  */
  262.             (void) dir_contents_file_exists_p (dc, (char *) 0);
  263.         }
  264.         }
  265.  
  266.       /* Point the name-hashed entry for DIR at its contents data.    */
  267.       dir->contents = dc;
  268.     }
  269.     }
  270.  
  271.   return dir;
  272. }
  273.  
  274. /* Return 1 if the name FILENAME is entered in DIR's hash table.
  275.    FILENAME must contain no slashes.  */
  276.  
  277. static int
  278. dir_contents_file_exists_p (dir, filename)
  279.      register struct directory_contents *dir;
  280.      register char *filename;
  281. {
  282.   register unsigned int hash;
  283.   register char *p;
  284.   register struct dirfile *df;
  285.   register struct dirent *d;
  286.  
  287.   if (dir == 0 || dir->files == 0)
  288.     /* The directory could not be stat'd or opened.  */
  289.     return 0;
  290.  
  291. #ifdef __MSDOS__
  292.   filename = dosify (filename);
  293. #endif
  294. #ifdef _AMIGA
  295.   filename = amigafy (filename);
  296. #endif
  297.  
  298.   hash = 0;
  299.   if (filename != 0)
  300.     {
  301.       if (*filename == '\0')
  302.     /* Checking if the directory exists.  */
  303.     return 1;
  304.  
  305.       for (p = filename; *p != '\0'; ++p)
  306.     HASH (hash, *p);
  307.       hash %= DIRFILE_BUCKETS;
  308.  
  309.       /* Search the list of hashed files.  */
  310.  
  311.       for (df = dir->files[hash]; df != 0; df = df->next)
  312.     if (streq (df->name, filename))
  313.       return !df->impossible;
  314.     }
  315.  
  316.   /* The file was not found in the hashed list.
  317.      Try to read the directory further.  */
  318.  
  319.   if (dir->dirstream == 0)
  320.     /* The directory has been all read in.  */
  321.     return 0;
  322.  
  323.   while ((d = readdir (dir->dirstream)) != 0)
  324.     {
  325.       /* Enter the file in the hash table.  */
  326.       register unsigned int newhash = 0;
  327.       unsigned int len;
  328.       register unsigned int i;
  329.  
  330.       if (!REAL_DIR_ENTRY (d))
  331.     continue;
  332.  
  333.       len = D_NAMLEN (d);
  334.       while (d->d_name[len - 1] == '\0')
  335.     --len;
  336.  
  337.       for (i = 0; i < len; ++i)
  338. #ifdef _AMIGA
  339.     HASH (newhash, tolower(d->d_name[i]));
  340. #else
  341.     HASH (newhash, d->d_name[i]);
  342. #endif
  343.       newhash %= DIRFILE_BUCKETS;
  344.  
  345.       df = (struct dirfile *) xmalloc (sizeof (struct dirfile));
  346.       df->next = dir->files[newhash];
  347.       dir->files[newhash] = df;
  348.       df->name = savestring (d->d_name, len);
  349.       df->impossible = 0;
  350.  
  351.       /* Check if the name matches the one we're searching for.  */
  352.       if (filename != 0
  353.       && newhash == hash && strieq (d->d_name, filename))
  354.     return 1;
  355.     }
  356.  
  357.   /* If the directory has been completely read in,
  358.      close the stream and reset the pointer to nil.  */
  359.   if (d == 0)
  360.     {
  361.       --open_directories;
  362.       closedir (dir->dirstream);
  363.       dir->dirstream = 0;
  364.     }
  365.  
  366.   return 0;
  367. }
  368.  
  369. /* Return 1 if the name FILENAME in directory DIRNAME
  370.    is entered in the dir hash table.
  371.    FILENAME must contain no slashes.  */
  372.  
  373. int
  374. dir_file_exists_p (dirname, filename)
  375.      register char *dirname;
  376.      register char *filename;
  377. {
  378.   return dir_contents_file_exists_p (find_directory (dirname)->contents,
  379.                      filename);
  380. }
  381.  
  382. /* Return 1 if the file named NAME exists.  */
  383.  
  384. int
  385. file_exists_p (name)
  386.      register char *name;
  387. {
  388.   char *dirend;
  389.   char *dirname;
  390.  
  391. #ifndef NO_ARCHIVES
  392.   if (ar_name (name))
  393.     return ar_member_date (name) != (time_t) -1;
  394. #endif
  395.  
  396.   dirend = rindex (name, '/');
  397.   if (dirend == 0)
  398. #ifndef _AMIGA
  399.     return dir_file_exists_p (".", name);
  400. #else
  401.     return dir_file_exists_p ("", name);
  402. #endif
  403.  
  404.   dirname = (char *) alloca (dirend - name + 1);
  405.   bcopy (name, dirname, dirend - name);
  406.   dirname[dirend - name] = '\0';
  407.   return dir_file_exists_p (dirname, dirend + 1);
  408. }
  409.  
  410. /* Mark FILENAME as `impossible' for `file_impossible_p'.
  411.    This means an attempt has been made to search for FILENAME
  412.    as an intermediate file, and it has failed.    */
  413.  
  414. void
  415. file_impossible (filename)
  416.      register char *filename;
  417. {
  418.   char *dirend;
  419.   register char *p = filename;
  420.   register unsigned int hash;
  421.   register struct directory *dir;
  422.   register struct dirfile *new;
  423.  
  424.   dirend = rindex (p, '/');
  425.   if (dirend == 0)
  426.     dir = find_directory (".");
  427.   else
  428.     {
  429.       char *dirname = (char *) alloca (dirend - p + 1);
  430.       bcopy (p, dirname, dirend - p);
  431.       dirname[dirend - p] = '\0';
  432.       dir = find_directory (dirname);
  433.       filename = p = dirend + 1;
  434.     }
  435.  
  436.   for (hash = 0; *p != '\0'; ++p)
  437.     HASH (hash, *p);
  438.   hash %= DIRFILE_BUCKETS;
  439.  
  440.   if (dir->contents == 0)
  441.     {
  442.       /* The directory could not be stat'd.  We allocate a contents
  443.      structure for it, but leave it out of the contents hash table.  */
  444.       dir->contents = (struct directory_contents *)
  445.     xmalloc (sizeof (struct directory_contents));
  446.       dir->contents->dev = dir->contents->ino = 0;
  447.       dir->contents->files = 0;
  448.       dir->contents->dirstream = 0;
  449.     }
  450.  
  451.   if (dir->contents->files == 0)
  452.     {
  453.       /* The directory was not opened; we must allocate the hash buckets.  */
  454.       dir->contents->files = (struct dirfile **)
  455.     xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
  456.       bzero ((char *) dir->contents->files,
  457.          sizeof (struct dirfile) * DIRFILE_BUCKETS);
  458.     }
  459.  
  460.   /* Make a new entry and put it in the table.    */
  461.  
  462.   new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
  463.   new->next = dir->contents->files[hash];
  464.   dir->contents->files[hash] = new;
  465.   new->name = savestring (filename, strlen (filename));
  466.   new->impossible = 1;
  467. }
  468.  
  469. /* Return nonzero if FILENAME has been marked impossible.  */
  470.  
  471. int
  472. file_impossible_p (filename)
  473.      char *filename;
  474. {
  475.   char *dirend;
  476.   register char *p = filename;
  477.   register unsigned int hash;
  478.   register struct directory_contents *dir;
  479.   register struct dirfile *next;
  480.  
  481.   dirend = rindex (filename, '/');
  482.   if (dirend == 0)
  483.     dir = find_directory (".")->contents;
  484.   else
  485.     {
  486.       char *dirname = (char *) alloca (dirend - filename + 1);
  487.       bcopy (p, dirname, dirend - p);
  488.       dirname[dirend - p] = '\0';
  489.       dir = find_directory (dirname)->contents;
  490.       p = dirend + 1;
  491.     }
  492.  
  493.   if (dir == 0 || dir->files == 0)
  494.     /* There are no files entered for this directory.  */
  495.     return 0;
  496.  
  497. #ifdef __MSDOS__
  498.   p = filename = dosify (p);
  499. #endif
  500. #ifdef _AMIGA
  501.   p = filename = amigafy (p);
  502. #endif
  503.  
  504.   for (hash = 0; *p != '\0'; ++p)
  505.     HASH (hash, *p);
  506.   hash %= DIRFILE_BUCKETS;
  507.  
  508.   for (next = dir->files[hash]; next != 0; next = next->next)
  509.     if (streq (filename, next->name))
  510.       return next->impossible;
  511.  
  512.   return 0;
  513. }
  514.  
  515. /* Return the already allocated name in the
  516.    directory hash table that matches DIR.  */
  517.  
  518. char *
  519. dir_name (dir)
  520.      char *dir;
  521. {
  522.   return find_directory (dir)->name;
  523. }
  524.  
  525. /* Print the data base of directories.    */
  526.  
  527. void
  528. print_dir_data_base ()
  529. {
  530.   register unsigned int i, dirs, files, impossible;
  531.   register struct directory *dir;
  532.  
  533.   puts ("\n# Directories\n");
  534.  
  535.   dirs = files = impossible = 0;
  536.   for (i = 0; i < DIRECTORY_BUCKETS; ++i)
  537.     for (dir = directories[i]; dir != 0; dir = dir->next)
  538.       {
  539.     ++dirs;
  540.     if (dir->contents == 0)
  541.       printf ("# %s: could not be stat'd.\n", dir->name);
  542.     else if (dir->contents->files == 0)
  543.       printf ("# %s (device %d, inode %d): could not be opened.\n",
  544.           dir->name, dir->contents->dev, dir->contents->ino);
  545.     else
  546.       {
  547.         register unsigned int f = 0, im = 0;
  548.         register unsigned int j;
  549.         register struct dirfile *df;
  550.         for (j = 0; j < DIRFILE_BUCKETS; ++j)
  551.           for (df = dir->contents->files[j]; df != 0; df = df->next)
  552.         if (df->impossible)
  553.           ++im;
  554.         else
  555.           ++f;
  556.         printf ("# %s (device %d, inode %d): ",
  557.             dir->name, dir->contents->dev, dir->contents->ino);
  558.         if (f == 0)
  559.           fputs ("No", stdout);
  560.         else
  561.           printf ("%u", f);
  562.         fputs (" files, ", stdout);
  563.         if (im == 0)
  564.           fputs ("no", stdout);
  565.         else
  566.           printf ("%u", im);
  567.         fputs (" impossibilities", stdout);
  568.         if (dir->contents->dirstream == 0)
  569.           puts (".");
  570.         else
  571.           puts (" so far.");
  572.         files += f;
  573.         impossible += im;
  574.       }
  575.       }
  576.  
  577.   fputs ("\n# ", stdout);
  578.   if (files == 0)
  579.     fputs ("No", stdout);
  580.   else
  581.     printf ("%u", files);
  582.   fputs (" files, ", stdout);
  583.   if (impossible == 0)
  584.     fputs ("no", stdout);
  585.   else
  586.     printf ("%u", impossible);
  587.   printf (" impossibilities in %u directories.\n", dirs);
  588. }
  589.  
  590. /* Hooks for globbing.    */
  591.  
  592. #include <glob.h>
  593.  
  594. /* Structure describing state of iterating through a directory hash table.  */
  595.  
  596. struct dirstream
  597.   {
  598.     struct directory_contents *contents; /* The directory being read.  */
  599.  
  600.     unsigned int bucket;    /* Current hash bucket.  */
  601.     struct dirfile *elt;    /* Current elt in bucket.  */
  602.   };
  603.  
  604. /* Forward declarations.  */
  605. static __ptr_t open_dirstream __P ((const char *));
  606. static const char *read_dirstream __P ((__ptr_t));
  607.  
  608. static __ptr_t
  609. open_dirstream (directory)
  610.      const char *directory;
  611. {
  612.   struct dirstream *new;
  613.   struct directory *dir = find_directory (directory);
  614.  
  615.   if (dir->contents == 0 || dir->contents->files == 0)
  616.     /* DIR->contents is nil if the directory could not be stat'd.
  617.        DIR->contents->files is nil if it could not be opened.  */
  618.     return 0;
  619.  
  620.   /* Read all the contents of the directory now.  There is no benefit
  621.      in being lazy, since glob will want to see every file anyway.  */
  622.  
  623.   (void) dir_contents_file_exists_p (dir->contents, (char *) 0);
  624.  
  625.   new = (struct dirstream *) xmalloc (sizeof (struct dirstream));
  626.   new->contents = dir->contents;
  627.   new->bucket = 0;
  628.   new->elt = new->contents->files[0];
  629.  
  630.   return (__ptr_t) new;
  631. }
  632.  
  633. static const char *
  634. read_dirstream (stream)
  635.      __ptr_t stream;
  636. {
  637.   struct dirstream *const ds = (struct dirstream *) stream;
  638.   register struct dirfile *df;
  639.  
  640.   while (ds->bucket < DIRFILE_BUCKETS)
  641.     {
  642.       while ((df = ds->elt) != 0)
  643.     {
  644.       ds->elt = df->next;
  645.       if (!df->impossible)
  646.         return df->name;
  647.     }
  648.       if (++ds->bucket == DIRFILE_BUCKETS)
  649.     break;
  650.       ds->elt = ds->contents->files[ds->bucket];
  651.     }
  652.  
  653.   return 0;
  654. }
  655.  
  656. void
  657. init_dir ()
  658. {
  659.   __glob_opendir_hook = open_dirstream;
  660.   __glob_readdir_hook = read_dirstream;
  661.   __glob_closedir_hook = (void (*) __P ((__ptr_t stream))) free;
  662. }
  663.